package logic

import (
	"context"
	"errors"
	"goods_srv/goods_srv"
	"goods_srv/internal/svc"
	"goods_srv/models/mysql"
	"goods_srv/models/redis"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsCreateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsCreateLogic {
	return &GoodsCreateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsCreateLogic) GoodsCreate(in *goods_srv.GoodsCreateRequest) (*goods_srv.GoodsCreateResponse, error) {
	// todo: add your logic here and delete this line
	goods := mysql.Goods{
		UserId:     int(in.UserId),
		GoodsName:  in.GoodsName,
		GoodsTitle: in.GoodsTitle,
		GoodsPrice: float64(in.GoodsPrice),
		GoodsStock: int(in.GoodsStock),
		GoodsCover: in.GoodsCover,
		GoodsIntro: in.GoodsIntro,
	}

	err := goods.Create()

	if err != nil {
		return nil, errors.New("商品发布失败")
	}

	redis.CreateStock(int(goods.ID), int(in.GoodsStock))

	return &goods_srv.GoodsCreateResponse{Id: int64(goods.ID)}, nil
}
